//
//  HRNextViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/27.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"CELL"
#import "HRNextViewController.h"
#import "HRYouXuanModel.h"
#import "HRNextModel.h"
#import "HRNextCell.h"
#import "HRNetPageViewController.h"
@interface HRNextViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrM;
@end

@implementation HRNextViewController
-(NSMutableArray *)arrM
{
    if (_arrM==nil) {
        _arrM=[NSMutableArray array];
    }
    return _arrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=self.model.name;
    self.navigationController.navigationBar.barTintColor=HRCOLOR(0, 200, 130);
    [self createLeftButton];
    self.tableView.rowHeight=88;
    [self setXib];
    [self sendRequestData];
    
    
    
}
//添加leftBtn
-(void)createLeftButton
{
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btn setBackgroundImage:[UIImage imageNamed:@"webview_back"] forState:UIControlStateNormal];
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=item;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)btnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
//注册Xib
-(void)setXib
{
    UINib *nib=[UINib nibWithNibName:@"HRNextCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
}
//请求网络数据
-(void)sendRequestData
{
    NSURLSession *session=[NSURLSession sharedSession];
    NSString *str=[NSString stringWithFormat:@"%@",self.model.url];
    NSURL *url=[NSURL URLWithString:str];
    
    NSURLSessionDataTask *dataTask=[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dataDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSArray *arr=dataDict[@"list"];
        for (NSDictionary *dict in arr) {
            HRNextModel *model=[HRNextModel modelWithDict:dict];
            [self.arrM addObject:model];
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
        }
        
    }];
    [dataTask resume];
    

}


#pragma mark - tableView的数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrM.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRNextModel *model=self.arrM[indexPath.row];
    HRNextCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.model=model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRNextModel *model=self.arrM[indexPath.row];
    HRNetPageViewController *netPage=[[HRNetPageViewController alloc] init];
    netPage.model=model;
    [self.navigationController pushViewController:netPage animated:YES];
}
@end
